package action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import service.TutorService;

//家教对订单的一系列操作
public class TutorAction {
	private int orderID; //由链接传入的id号
	private int empID;
	private int tutorID;
	private List emps;
	
	//订单号参数设置
	public void setParam() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String sId = (String)request.getParameter("id");
		System.out.println("得到的订单编号为:"+sId); //测试
		orderID = Integer.parseInt(sId); //得到订单号
	}
	
	//双参数设置-用户家教对招聘信息的发起申请
	public void setParams() {
		HttpServletRequest request = ServletActionContext.getRequest();
		//得到用户id 正常登陆取值正常
		HttpSession session = request.getSession();
		tutorID = (int) session.getAttribute("userID");
		System.out.println("得到的用户编号为:"+tutorID); //测试   //这里应该有一段空值的判定

		String sId = (String)request.getParameter("id");
		System.out.println("得到的订单编号为:"+sId); //测试
		empID = Integer.parseInt(sId); //得到订单号
	}
	//家教接受订单
	public String accept() {
		setParam();
		TutorService ts = new TutorService();
		if(ts.accept(orderID)) {
			return "success";
		}else {
			return "failed";
		}
	}
	//家教拒绝订单
	public String refuse() {
		setParam();
		TutorService ts = new TutorService();
		if(ts.refuse(orderID)) {
			return "success";   
		}else {
			return "failed";
		}
	}
	public void setOrderID(int orderID) {
		this.orderID = orderID;
	}
	//家教结束订单-订单结束
	public String finishCourse() {
		
		TutorService ts = new TutorService();
		if(ts.finishToCourse(orderID)) {
			return "success";   
		}else {
			return "failed";
		}
	}
	
	//家教申请用户发出的招聘信息-跳转到自己的订单中心
	public String order() {
		setParams();
		TutorService ts = new TutorService();
		//更新招聘信息状态
		ts.place(tutorID, empID);
		if(ts.getOrders(tutorID)) {
			emps = ts.getList();
		}
		return "success";
	}
	
	//家教取消招聘信息的需求
	public String cancel() {
		setParam();
		TutorService ts = new TutorService();
		if(ts.cancel(orderID)) {
			return "success";
		}
		return "fail";
	}
	
	//家教确认接受的招聘完成
	public String finish() {
		setParam();
		TutorService ts = new TutorService();
		if(ts.finish(orderID)) {
			return "success";
		}
		return "fail";
	}
}
